(function(){
	angular.module('app')
		.controller('ctrl.column',['$scope','$uibModalInstance','config','http','param',columnCtrl])
		.service('myColumn',['$rootScope','$uibModal','http', 'myUtils', pageCtrl])
		function columnCtrl($scope,$uibModalInstance,config,http,param){
			var vm = $scope.vm = {};
			angular.extend(vm,{
				init : function(){
					vm.navList=[{id:0,name:'全部'}];
					vm.checkList=new Array;
					if( param ){
						vm.checkList=[];
						vm.parma = param || {};
						vm.checkList=param.checkList;
					}
					vm.getInfo();
				},
				getInfo : function(fid){
					/***获取投放点信息***/
					if(param.checkList.type && param.checkList.type == 'vote'){
						var url=config.getUrl('vote_category');
						http(url,{id : fid})
							.then(function (data) {
								if( data && ( data.error_code || data.error_message)){
									console.warn(data.error_message || '访问错误');
									return false;
								}else {
									vm.columnList = data.data;
									if(!fid){
										vm.fname = '全部';
									}
								}
							})
					}else {
						var url = config.getUrl('content_column');
						url = url.replace('{fid?}',fid );

						http( url )
							.then( function( data ){
								if( data.data && data.data[0]){
									vm.columnList = data.data;
								}
							})
					}
				},

				delCol:function(item,index){
					vm.checkList.splice(index,1);
				},
				check : function( item){
					var i=0;
					angular.forEach( vm.checkList, function(checkItem, key){
						if(checkItem.column_id == item.id){
							i=1;
						}
					});
					if(i == 0){
						if(item.is_last == 1){
							item.column_name = item.name;
							item.column_id = item.id;
							if(vm.checkList[0]&&param.checkList.type && param.checkList.type == 'vote'){
								vm.checkList[0] = item;
							}else {
								vm.checkList.push(item);
							}
						}else{
							vm.fname = item.name;
							vm.navList.push(item);
							vm.getInfo(item.id);
						}
					}
					
				},
				goNew:function(item,index){
					vm.navList=vm.navList.slice(0,index+1);
					vm.fname = item.name;
					vm.getInfo(item.id);
				},
				save : function(){
					if( vm.checkList && vm.checkList[0] ){
						var param = {
							checkList : vm.checkList,
							navList:vm.navList
						};
						console.log(param,44444);
						$uibModalInstance.close( param );
					}
					else{
						$uibModalInstance.dismiss('cancel');
					}
				},
				cancel : function(){
					$uibModalInstance.dismiss('cancel');
				}
			});
			vm.init();
		}
		function pageCtrl($rootScope,$uibModal,http, myUtils){
			return{
				showColumn  : function( cbk,param  ){
					 var modalInstance = $uibModal.open({
				          templateUrl: "../views/mxu/common/tpl/column.html",
				          controller: 'ctrl.column',
				          resolve: {
				           param: function () {
				              return param ;
				            }
				          }
			         });
			         modalInstance.result.then(function ( list ) {
			         	$.isFunction( cbk ) && cbk( list );
			        })
			   }
		  }
		}
})();